#include "mq_connection.hpp"
#include "mq_worker.hpp"
#include "muduo/net/TcpConnection.h"
#include <google/protobuf/map.h>
#include <memory>
#include <string>
#include <format>

int main()
{
    // 1. 实例化异步工作线程池对象、连接对象
    mqclient::AsyncWorkerPtr worker = std::make_shared<mqclient::AsyncWorker>();
    mqclient::ConnectionPtr conn = std::make_shared<mqclient::Connection>("127.0.0.1", 8080, worker);
    // 2. 打开信道
    mqclient::ChannelPtr channel = conn->openChannel();
    // 3. 通过信道提供服务
    std::string exchange1 = "exchange1";
    std::string queue1 = "queue1";
    std::string queue2 = "queue2";
    //   a. 声明 exchange1, 广播交换
    google::protobuf::Map<std::string, std::string> empty_map;
    // channel->declareExchange(exchange1, proto::message::ExchangeType::FANOUT, true, false, empty_map);   
    // channel->declareExchange(exchange1, proto::message::ExchangeType::DIRECT, true, false, empty_map);
    channel->declareExchange(exchange1, proto::message::ExchangeType::TOPIC, true, false, empty_map);
    //   b. 声明 queue1、queue2
    channel->declareQueue(queue1, true, false, false, empty_map);
    channel->declareQueue(queue2, true, false, false, empty_map);
    //   c. 绑定 exchange1-queue1/exchange1-queue2
    channel->queueBind(exchange1, queue1, "queue1");
    channel->queueBind(exchange1, queue2, "news.music.#");
    // 4. 循环向交换机发送消息
    for (int i = 0; i < 10; ++i) {
        proto::message::BasicProperties properties;
        properties.set_id(helper::UUIDHelper::uuid());
        properties.set_delivery_mode(proto::message::DURABLE);
        // properties.set_routing_key(queue1);
        properties.set_routing_key("news.music.pop");       // queue2 收到
        channel->basicPublish(exchange1, &properties, std::format("this is message[news.music.pop][{}]", i));
    }

    proto::message::BasicProperties properties;
    properties.set_id(helper::UUIDHelper::uuid());
    properties.set_delivery_mode(proto::message::DURABLE);
    properties.set_routing_key("news.music.ancient");
    channel->basicPublish(exchange1, &properties, "Only the queue2 can be receive this message[news.music.ancient].");

    properties.set_routing_key("news.sport.football");
    channel->basicPublish(exchange1, &properties, "No queue can receive this message[news.sport.football]."); 

    // 5. 关闭信道
    conn->closeChannel(channel);
    return 0;
}